package com.baidu.controller;

import com.alibaba.fastjson.JSON;
import com.baidu.entity.Week01;
import com.baidu.service.Week01Service;
import com.baidu.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by mac on 2017/12/29.
 */
@Controller
public class WeekController {
    @Autowired
    @Qualifier("weekService")
    Week01Service week01Service;

    @Autowired
    private KafkaTemplate<Object, String> template;
    @RequestMapping("/test")
    @ResponseBody
    public List<Week01> test(){

        return week01Service.selectByCondition(new Week01());
    }
    @RequestMapping("/getWeekList")
    public String getWeekList(ModelMap map,Week01 week01){
        int count = week01Service.selectCountByCondition(week01);
        Page page = new Page(week01.getCpage(),count,3);
        week01.setPageSize(3);
        week01.setStartPage(page.getStartPage());
        List<Week01> weekList = week01Service.selectByCondition(week01);
        map.put("weekList",weekList);
        map.put("page",page);
        this.template.send("1510D", JSON.toJSONString(weekList));
//		this.template.send("1510D", "foo2");
//		this.template.send("1510D", "foo3");
        return "list";
    }
    @RequestMapping("/toAddWeek")
    public String toAddWeek(){

        return "toAddWeek";
    }
    //增加
    @RequestMapping("/AddWeek")
    @ResponseBody
    public String AddWeek(Week01 week01) throws Exception {

        Integer result = week01Service.insertSelective(week01);
        if(result!=1){
            return "false";
        }else{
            return "true";
        }
    }
    //删除
    @RequestMapping(value = "delWeek",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String delWeek(Week01 week01) throws Exception {

        Integer result = week01Service.deleteByPrimaryKey(week01);
        if(result!=1){

            return "删除失败，请重试！！";
        }else{

            return "删除成功";
        }
    }
    //跳转修改页面
    @RequestMapping("/toUpdateWeek")
    public String toUpdateWeek(ModelMap map, int id){
        Week01 week = week01Service.findById(id);
        map.put("week",week);
        return "UpdateWeek";
    }
    //修改页面
    @RequestMapping("/updateWeek")
    @ResponseBody
    public String updateWeek(ModelMap map, Week01 week){
        Integer integer = week01Service.updateByPrimaryKeySelective(week);
        if(integer!=1){

            return "false";
        }else{

            return "true";
        }

    }
}
